//
//  XHRTextFieldRightView.m
//  快医
//
//  Created by 胥鸿儒 on 16/7/5.
//  Copyright © 2016年 xuhongru. All rights reserved.
//

#import "XHRTextFieldRightView.h"

@interface XHRTextFieldRightView()
/**记录当前选中的按钮*/
@property(nonatomic,weak)UIButton *selectedButton;
/**记录点击的按钮是不是接受过治疗的按钮*/

@end

@implementation XHRTextFieldRightView

static BOOL _treated;

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}
- (void)setType:(XHRTextFieldRightViewType)type
{
    _type = type;
    [self setupChildView];
}

- (void)setupChildView
{
    if (self.type == XHRTextFieldRightViewTypeSigleButton) {
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"illness_img_line.png"]];
        [self addSubview:imageView];
        
        [imageView makeConstraints:^(MASConstraintMaker *make) {
            make.leading.offset(0);
            make.top.offset(3);
            make.bottom.offset(-3);
            make.width.equalTo(1);
        }];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"illness_button_input_blue.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(didClickMoreButton:) forControlEvents:UIControlEventTouchUpInside];
        button.adjustsImageWhenHighlighted = NO;
        
        [self addSubview:button];
        
        [button makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.leading.top.bottom.trailing.offset(0);
        }];
    }
    else if (self.type == XHRTextFieldRightViewTypeDoubleButtonIsSure)
    {
        [self setupDoubuleButtonWithLeftTitle:@"已确诊" rightTitle:@"症状疑似"];
    }
    else if (self.type == XHRTextFieldRightViewTypeDoubleButtonIsTreated)
    {
        [self setupDoubuleButtonWithLeftTitle:@"接受过" rightTitle:@"未接受过"];
    }
}
- (void)setupDoubuleButtonWithLeftTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    [leftButton setTitle:leftTitle forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
    leftButton.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 0);
    leftButton.adjustsImageWhenHighlighted = NO;
    leftButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [leftButton addTarget:self action:@selector(didClickSelectButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftButton];
    [leftButton makeConstraints:^(MASConstraintMaker *make) {
        make.leading.offset(0);
        make.centerY.equalTo(self);
        make.width.equalTo(70);
    }];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    
    [rightButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    [rightButton setTitle:rightTitle forState:UIControlStateNormal];
    [rightButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
    rightButton.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
    rightButton.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 0);
    [rightButton addTarget:self action:@selector(didClickSelectButton:) forControlEvents:UIControlEventTouchUpInside];
    rightButton.adjustsImageWhenHighlighted = NO;
    
    [self addSubview:rightButton];
    
    [rightButton makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.leading.equalTo(leftButton.trailing).offset(10);
        make.width.equalTo(75);
    }];
}
- (void)setSelectedButton:(UIButton *)selectedButton
{
    if ([_selectedButton isEqual:selectedButton]) return;
    _selectedButton.selected = NO;
    selectedButton.selected = YES;
    _selectedButton = selectedButton;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
}
#pragma mark -
#pragma mark - 监听按钮的点击
- (void)didClickMoreButton:(UIButton *)sender
{

    if (self.loadMoreBlock) {
        self.loadMoreBlock();
    }
}
- (void)setIsClickTextField:(BOOL)isClickTextField
{
    _isClickTextField = isClickTextField;
    [self didClickMoreButton:nil];
    
}

- (void)didClickSelectButton:(UIButton *)sender
{
    self.selectedButton = sender;
    if ([sender.currentTitle isEqualToString:@"接受过"]) {
        _treated = YES;
    }
    else if([sender.currentTitle isEqualToString:@"未接受过"])
    {
        _treated = NO;
    }
    
    if (self.rightViewBlock) {
        self.rightViewBlock(_treated,sender.currentTitle);
    }
    
}
@end
